use crate::pool;
use rbatis::{crud, impl_select, impl_select_page, Page, rbdc::{self, db::ExecResult}, sql::{self, PageRequest}};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TbNetPoint {
    pub id: Option<u64>,
    pub appid: Option<String>,
    pub device_id: Option<String>,
    pub ip: Option<String>,
    pub mac: Option<String>,
    pub name: Option<String>,
    pub last_time: Option<i64>,
    pub enabled: Option<u32>,
}

crud!(TbNetPoint {});
impl_select!(TbNetPoint {select(appid: &pilota::FastStr, device_id: &pilota::FastStr) -> Option =>
    "`where appid=#{appid} and device_id=#{device_id}`"
});
impl_select_page!(TbNetPoint {select_page() => ""});

impl TbNetPoint {
    pub async fn get_netpoints(
        page: &crate::common::QueryPage,
    ) -> Result<Page<TbNetPoint>, rbdc::Error> {
        TbNetPoint::select_page(pool!(), &PageRequest::from(page)).await
    }

    pub async fn update_netpoint(data: &TbNetPoint) -> Result<ExecResult, rbdc::Error> {
        TbNetPoint::update_by_column(pool!(), data, "id").await
    }

    pub async fn delete_netpoint(
        data: &crate::handlers::netpoint::Delete,
    ) -> Result<ExecResult, rbdc::Error> {
        TbNetPoint::delete_by_column(pool!(), "id", data.id).await
    }

    pub async fn query_netpoint_by_appid_and_deviceid(
        appid: &pilota::FastStr,
        device_id: &pilota::FastStr,
    ) -> Result<Option<TbNetPoint>, rbdc::Error> {
        TbNetPoint::select(pool!(), appid, device_id).await
    }

    pub async fn insert_netpoint(netpoint: &TbNetPoint) -> Result<ExecResult, rbdc::Error> {
        TbNetPoint::insert(pool!(), netpoint).await
    }
}
